'use strict';

angular.module('app')
	.controller('userController', [ '$rootScope', '$scope', '$timeout', '$http', '$state', 'toaster',
	                                function($rootScope, $scope,$timeout, $http, $state, toaster) {
		$scope.title = '操作员管理';
        $scope.param = { };
        $scope.loading = false;
        
        $scope.regionName = $state.params.regionName;
    	$scope.param.regionId = $state.params.regionId;

		$scope.search = function () {
	        $scope.loading = true;
	        if($scope.pageFlag != 1){//未点击翻页
			  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
			  	}else{//点击翻页
			  	   $scope.pageFlag = 2;//将开关值设置为2
			  	}
			$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/user/read/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}

		//从本地获取localstorage数据，对权限按钮做隐藏
		var personAuth =localStorage.getItem("PersonAuth");//获取PersonAuth的值
		personAuth = personAuth.split(',');
		for(var i = 0;i<personAuth.length;i++){
			if(personAuth[i]=='sys.base.user.read'){
				$scope.read = true;
			}
			if(personAuth[i]=='sys.base.user.add'){
				$scope.add = true;
			}
			if(personAuth[i]=='sys.base.user.update'){
				$scope.update = true;
			}
			if(personAuth[i]=='sys.base.user.delete'){
				$scope.dele = true;
			}
		}

		$scope.search();
		
		$scope.clearSearch = function() {
			$scope.param.keyword= null;
			$scope.search();
		}
		
		$scope.disableItem = function(id, enable,user_name) {
			if (confirm('确定要禁用' + '[' + user_name + ']' + '吗?')) {
				$scope.loading = true;
				$scope.usparam = {};
	    		$scope.usparam.id= id;
	    		$scope.usparam.enable= enable;
	    		$.ajax({
					type: 'POST',
		            dataType: 'json',
					contentType:'application/json;charset=UTF-8',
					url : '/user/upOpUserStatus',
					data: angular.toJson($scope.usparam)
				}).then(function(result) {
					$scope.loading = false;
					if(result.httpCode ==200){//成功
	                	toaster.clear('*');
	                    toaster.pop('success', '', "更新成功");
	                    $scope.search();
	                }else{
	                    toaster.clear('*');
	                    toaster.pop('error', '', result.msg);
	                    $scope.isDisabled = false;
	                }
				});
			}
		}

		$scope.delItem = function(id,name) {
			$scope.loading = true;
			if (confirm("确 定 删 除 ["+name+"]？")==true)
			{
				$.ajax({
					type: 'DELETE',
					dataType: 'json',
					contentType:'application/json;charset=UTF-8',
					url : '/user/delete',
					data: angular.toJson({'id': id})
				}).then(function(result) {
					$scope.loading = false;
					if (result.httpCode == 200) {
						toaster.clear('*');
						toaster.pop('success', '', "删除成功");
						$scope.search();
					} else {
						toaster.pop('false', '', "删除失败");
					}
					$scope.$apply();
				});
			}else{
				$scope.loading = false;
				console.log('取消删除');
			}
		};

		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
        };

        $scope.getTreeList=function(){ 
          	 $.ajax({
      				type: 'POST',
      	            dataType: 'json',
      				contentType:'application/json;charset=UTF-8',
      				url : '/dac/regionManage/region/tree',
      				data: angular.toJson($scope.param)
      			}).then(function(result){
      				 if(result.httpCode ==200){//成功
                          $scope.angularTreeList=[];  
                          //查询树  
                          $scope.orgTypeList=result.data;    
                          //创建根节点  
                          var root={};  
                          root.regionname="区域列表";  
                          root.level=0;  
                          root.sequence=1;  
                          root.parentid=-1;
                          root.dragEnabled = false;
                          root.children=result.data;  
                          $scope.angularTreeList.push(root);
                          toaster.clear('*');
                          toaster.pop('success', '', "加载成功");
                      }else{
                          toaster.clear('*');
                          toaster.pop('error', '', result.msg);
                          $scope.isDisabled = false;
                      }
                      $scope.$apply();
      			});
          }  

           $scope.getTreeList();
        
        //树点击事件
        $scope.clickRegion = function(regionId,regionName){
        	$scope.regionName = regionName;
        	$scope.param.regionId=regionId;
        	$scope.search();
        }

        $scope.showMsg = function(msg){
        	alert(msg);
        }
        
        $scope.loadUserType = function () {
    		$scope.uparam = {};
    		$scope.uparam.key='USERTYPE';
    		$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dic/queryDicByType',
				data: angular.toJson($scope.uparam)
			}).then(function(result) {
				if (result.httpCode == 200) {
					$scope.userTypes = result.data;
					$scope.$apply();
				}
				
			});
    	}
    	
    	$scope.loadUserType();

} ]);